//
//  HELCourseListVC.m
//  sanda
//
//  Created by wsj2015 on 16/8/2.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELCourseListVC.h"
#import "HELNocontentView.h"
#import "HELSearchResultsModel.h"
#import "HELCourseDetailVC.h"
#import "HELDataCommonCell.h"


static NSInteger currentPage = 1;

@interface HELCourseListVC ()

@end

@implementation HELCourseListVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)configUI{
  
    

    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 24);
    [self.tableView registerNib:[UINib nibWithNibName:@"HELDataCommonCell" bundle:nil] forCellReuseIdentifier:@"HELDataCommonCell"];
    
    defineWeakSelf;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf requestSearchDataisRecover:YES];
        
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf requestSearchDataisRecover:NO];
    }];
    [weakSelf requestSearchDataisRecover:YES];
    
  
}

#pragma mark -查询数据
- (void)requestSearchDataisRecover:(BOOL)isRecover{
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"hospitalId"] =@(-1);
   
    params[@"pageSize"] = @(10);
    
    if (isRecover) {
        currentPage =1;
    }else{
        currentPage ++;
    }
    
    params[@"currentPage"] =@(currentPage);
    
 

    
    self.tableView.loading = YES;
    weakSelf_define;
    [[HELNetworkManager sharedMg] POST:kSearchStudyDat_url parameters:params success:^(id responseObject) {
        
        
        if (isRecover) {
            [weakSelf.dataArray removeAllObjects];
        }
        weakSelf.tableView.loading = NO;
        NSArray *arr =  responseObject[@"rows"];
        
        if ([arr isKindOfClass:[NSArray class]]) {
            
            [weakSelf.dataArray addObjectsFromArray:[HELSearchResultsModel mj_objectArrayWithKeyValuesArray:responseObject[@"rows"]]];
            
        }
        if (weakSelf.dataArray.count<=0) {
            weakSelf.tableView.loading = NO;
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        
    } failure:^(NSString *err) {
        weakSelf.tableView.loading = NO;
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataArray.count;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELDataCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELDataCommonCell" forIndexPath:indexPath];
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = kMainColor;
    bgView.frame =CGRectMake(0, 0, kScreenWidth, 87);
    cell.selectedBackgroundView = bgView;
    
    cell.modle = self.dataArray[indexPath.row];
    
    return cell;
    
}
    
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        
  
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        //进入资料详情
        
       weakSelf_define;
 
        HELCourseDetailVC *vc =[HELCourseDetailVC new];
        __block  HELSearchResultsModel *model =  self.dataArray[indexPath.row];
        vc.courseId =[model.id integerValue];
        vc.callBack = ^(HELCourseDetailModel *newModel){
            [weakSelf requestSearchDataisRecover:YES];
        };
        [self.parentNav pushViewController:vc animated:YES];

    

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 87;
}

@end
